var bulk_page_readOnly = false;
var BulkContent = Backbone.Model.extend({
	schema : bulkload.sections
});

var bulk_con = new BulkContent();
var bulk_asset = new Backbone.Asset({
	model : bulk_con,
	readOnly : bulk_page_readOnly
}).render();
	

var bulkInit = function(){
//	$(".panel-body").eq(1).append(bulk_asset.el);
//	$("#container_fileload .field-priority").css("display", "none");
	
	  //
	var prio = $(".field-priority").parent();
    prio.on({
		"user": this.userUpdate
	});
	Auth.ctrl.push(prio);
	prio.trigger("user");
	
	//prio.css("display", "none");
	

	// Initialize form data, for file type select
	//TODO:

	$("#container_fileload form").bind('fileuploadsubmit', function(e, data) {			

	});

	$("#container_fileload form").fileupload({
		url : util.sfbase+"/rest/fileUpload",   //"upload", //"../../rest/fileUpload",//
		//dataType : 'json',
		add : function(e, data) {
			$("#progress").attr("class", "span3 .fileupload-processing .fileupload-loading");
			//data.submit();
			var jqXHR = data.submit().success(function(result, textStatus, jqXHR) {
				var val = jqXHR.responseText;
				console.log("responseText: " + val);
				console.log("result: " + result);
				// var val = "{\"flag\":\"0\",\"status\":\"SUBMITTED\",\"filename\":\"GSI_APP-20120830-164910.csv\",\"destination\":\"/upload\",\"size\":\"297\"}";
				// var obj = JSON.parse(val);
				var table = asset.getTables().BULKLOAD;
				//table.saveRow(JSON.parse(result));
				result = JSON.parse(result);
				result.type = $("#type :selected").text();
				table.saveRow(result);
			}).error(function(jqXHR, textStatus, errorThrown) {
				alert("error");
				console.log(errorThrown);
			}).complete(function(result, textStatus, jqXHR) {
				console.log("complete");
				setTimeout("$('#progress').attr('class','span3 fileupload-progress fade')", 2000);
			});
		},
		// Callback for upload progress events:
		progress : function(e, data) {
			var progress = parseInt(data.loaded / data.total * 100, 10);
			$('#progress .bar').css('width', progress + '%');
		},
		done : function(e, data) {
			$.each(data.result, function(index, file) {
				$('<p/>').text(file.name).appendTo(document.body);
			});				
		}
	});

	var fileLoadHistory = function() {

		var createby = $("#createby").val();
		var createdate = $("#createdate_input").val();
		var modifydate = $("#modifydate_input").val();
		var priority = $("#prioritySearch").val();

		var datastr = "{\"createby\":\"" + createby + "\",\"createdate\":\"" + createdate + "\",\"modifydate\":\"" + modifydate + "\",\"priority\":\"" + priority + "\"}";
		var data = JSON.parse(datastr);

		$.ajax({
			url : util.sfbase+"/rest/fileUpload/loadHistory",  // "loadHistory", //"../../rest/fileUpload/loadHistory",//"../../rest/fileUpload/userHistory",
			type : "POST",
			dataType : "json",
			data : data,
			error : function(xhr, ajaxOptions, thrownError) {
//				$.unblockUI();
				alert("error:" + thrownError);
				return false;
			},
			success : function(data) {
				if (data.flag == 0) {
					var val = data.val, table = asset.getTables().BULKLOADHISTORY;
					table.setValue = function(value) {
						if (value && value.length > 0) {
							this.removeAll();
							//this.addFirst();
							var collection = new BTS.SortableList(value);
							this.collection = collection;
							this.collection.forEach(this.add, this);
						} else {
							this.removeAll();
						}
					};
					if (val && val.length > 0) {
						table.setValue(val);
					} else {
						table.setValue([]);
					}
				} else if (data.flag == 1) {
					alert(data.error.info);
				} else {
					alert("results format error");
				}
			}
		});
	};

	$("#btnFHSearch").bind('click', fileLoadHistory);
};